/**
 * 首页数据状态管理，高德地图接口
 */
import utils  from '../uview-ui/index.js'
import api from '../api/index.js'
const module = {
  namespaced: true,
  state:()=>({
	baselist:[
		// {
		// 	price: 35,
		// 	title: '北国风光，千里冰封，万里雪飘',
		// 	shop: '李白杜甫白居易旗舰店',
		// 	image: 'http://pic.sc.chinaz.com/Files/pic/pic9/202002/zzpic23327_s.jpg',
		// },
		// {
		// 	price: 75,
		// 	title: '望长城内外，惟余莽莽',
		// 	shop: '李白杜甫白居易旗舰店',
		// 	image: 'http://pic.sc.chinaz.com/Files/pic/pic9/202002/zzpic23325_s.jpg',
		// },
		// {
		// 	price: 385,
		// 	title: '大河上下，顿失滔滔',
		// 	shop: '李白杜甫白居易旗舰店',
		// 	image: 'http://pic2.sc.chinaz.com/Files/pic/pic9/202002/hpic2119_s.jpg',
		// },
		// {
		// 	price: 784,
		// 	title: '欲与天公试比高',
		// 	shop: '李白杜甫白居易旗舰店',
		// 	image: 'http://pic2.sc.chinaz.com/Files/pic/pic9/202002/zzpic23369_s.jpg',
		// },
	],
    list:[],//首页加载列表数据
	page:1,
	loadStatus:'loadmore',
  }),
  mutations: {
	refreshList(state){
		console.log("refreshList")
		state.list = [];
		state.page = 1;
	},
    getList(state,data) {
		if(data && data.length){
			data.forEach((item,index)=>{
				if(item.photos.length){
					item.cover = item.photos[0].url;
				}else{
					item.cover = `https://picsum.photos/300?random=${index}`;
				}
				item.type = item.type.split(";");
			})
			state.list = [...state.list,...data];
		}
		console.log("state.list",state);
		// 这里的 `state` 对象是模块的局部状态
    }
  },
  actions:{
	  // state为当前模块的数据，commit触发mutation方法，rootState是根节点状态，info是传参
	getList({state,commit,rootState},keywords){
		state.loadStatus = 'loading';
		if(uni.getStorageSync('city')){
			api.keywordSearch({
				// city:city?city:uni.getStorageSync('city'),
				page:state.page,
				keywords:keywords?keywords:'郑州',
				offset:10,
			}).then(res=>{
				console.log("keywordSearch",res);
				if(res.data.status == 1){
					state.page++;
					commit('getList',res.data.pois);
				}
			})
		}
	},
	refreshList({state,commit,rootState},keywords){
		commit('refreshList')
		state.loadStatus = 'loading';
		setTimeout(() => {
			if(uni.getStorageSync('city')){
				api.keywordSearch({
					// city:city?city:'郑州',
					page:state.page,
					keywords:keywords?keywords:'郑州',
					offset:10,
				}).then(res=>{
					console.log("keywordSearch",res);
					if(res.data.status == 1){
						state.page++;
						commit('getList',res.data.pois);
					}
				})
			}
			state.loadStatus = 'loadmore';
		}, 1000)		
	}
  },
  getters: {
    getloadStatus(state){
      return state.loadStatus;
    },
	getList(state){
	  return state.list
	}
  }
}
export default module